/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.session;

import com.jb.web.bean.NonDeletable;
import com.jb.web.bean.UIComponentImpl;
import com.jb.web.context.WebContext;

import com.jb.faces.component.NamingContainer;


/**
 * Implements server clipboard for copy, cut
 * and paste operation on components.
 */
public class Clipboard {
    public static final int COPY = 0;
    public static final int CUT = 1;
    public UIComponentImpl object;
    int operation;

    public void clear() {
        object = null;
    }

    public void copy(WebContext pCtx, UIComponentImpl object) {
        if (object instanceof NonDeletable) {
            return;
        }

        operation = COPY;
        this.object = object;
    }

    public boolean copyComponent(WebContext pCtx, UIComponentImpl parent, UIComponentImpl child) {
        try {
            if (!(parent instanceof NamingContainer) || !parent.isDesignable()) {
                pCtx.addErrorMessage("copyComponent", "Parent must be a valid component to copy to");
                return false;
            }

            if (child == null) {
                pCtx.addErrorMessage("copyComponent", "No Component to copy");
                return false;
            }

            UIComponentImpl copy = (UIComponentImpl) child.clone(pCtx, true, operation == COPY);

            parent.addChild(copy);
            parent.onPaste(copy);
            return true;
        } catch (Exception e) {
            pCtx.addErrorMessage(this, e.getMessage());

            return false;
        }
    }

    /**
     * Places object into clipboard, and mark the operation as CUT
     * @param pCtx
     * @param object
     */
    public void cut(WebContext pCtx, UIComponentImpl object) {
        if (object instanceof NonDeletable) {
            return;
        }

        this.object = object;
        operation = CUT;
    }

    /**
     * Copy object in the clipboard to the new parent, if success, remove it from
     * from old parent, and remove from the clip board.
     * @param pCtx
     * @param parent
     */
    public void paste(WebContext pCtx, UIComponentImpl parent) {
        if (object == null) {
            return;
        }
        if (copyComponent(pCtx, parent, object) && (operation == CUT)) {
            object.getParent().getChildren().remove(object);
           // pCtx.getSession().setSelected(null);
            object = null;
        }
    }
}

//
